// components/supplierSelection/index.js
const common = require('../../utils/common.js');
Component({
  options: {
    styleIsolation: 'shared',
  },
  /**
   * 组件的属性列表
   */
  properties: {
    // 显示-隐藏
    show: {
      type: Boolean,
      value: false,
    },
    multiple: { // 是否多选
      type: Boolean,
      value: false,
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    list_params:{
      needPage:true,
      current:1   ,
      size:100,
      pages:0,
      parame:[],
      selectParame:[],
      orderBy:"",
      selectParameter:{
          keyword: '', // 物料编码
          tabValue: "all",
          supplierShortName: ''// 供应商简称
      }
    },
    totalCount: 0,
    // 搜索条件
    search_keyword: '',
    //供应商
    defaultFieldNames: {
      label: "supplierCodeAndName",
      value: "supplierId",
    },
    defaultFieldNames2: {
      label: "supplierCodeAndName",
      value: "id",
    },
    selectList: [], // 选中的数据
    supplyList: [], // 供应商列表
  },
  ready() {
    // 组件布局完成后执行
    this.getSupplyList();
  },
  /**
   * 组件的方法列表
   */
  methods: {
    //获取供应商列表
    getSupplyList() {
      common
      .request_api(
        "/api/erp/mdSupplier/querySupplierPage", this.data.list_params,
        true,
        "post"
      )
      .then((res) => {
        const data = res.data.records.map(item => {
          item.supplierCodeAndName = item.supplierCode + '/' + item.supplierShortName
          return {
            ...item
          }
        })
        let supplyList =this.data.supplyList.concat(data)
        this.setData({
          supplyList: supplyList,
          totalCount: res.data.total
        });
      });
    },
    supplyCancelSelEvent(e) {
      let {
        selectedIndex
      } = e.detail;
      let list = this.data.selectList;
      selectedIndex.forEach((index, k) => {
        let removeElement = list.splice(index, 1);
        this.data.supplyList.unshift({
          id: removeElement[0].supplierId,
          supplierShortName: removeElement[0].supplierShortName,
          supplierCodeAndName: removeElement[0].supplierCodeAndName,
        });
      });
      this.setData({
        ["selectList"]: list,
        supplyList: this.data.supplyList,
      });
      this.resetSupplyList();
      this.handleSelect()
    },
    //重置供应商对应数据
    resetSupplyList() {
      let supplyList = this.data.supplyList
      const selectList = this.data.selectList
      supplyList = supplyList.filter(i => {
        return !selectList.some(p => p.supplierId === i.id)
      })
      this.setData({
        supplyList: supplyList
      });
    },
    //供应商名称搜索框输入框内容变更
    search_keyword_change(e) {
      let {
        value
      } = e.detail;
      this.setData({
        search_keyword: value,
        'list_params.selectParameter.supplierShortName': value
      });
    },
    handelSearchConfirm() {
      this.setData({
        supplyList: []
      });
      this.getSupplyList();
    },
    searchClear() {
      this.setData({
        search_keyword: "",
        supplyList: [],
        'list_params.current': 1,
        'list_params.selectParameter.supplierShortName': ''
      });
      this.getSupplyList();
    },
    //点击选择供应商
    supplySelEvent(e) {
      let sel_ids = e.detail.value;
      let selectList = this.data.selectList
      let supplyList = this.data.supplyList
      if (this.data.multiple) {
        for (let index = 0; index < sel_ids.length; index++) {
          let findindex = supplyList.findIndex(
            (v) => v.id == sel_ids[index]
          );
          if (findindex != -1) {
            let findindex2 = selectList.findIndex(
              (v) => v.id == sel_ids[index]
            );
            if (findindex2 == -1) {
              let is_sx = selectList.findIndex(
                (v) => v.firstChooseIndex == 1
              );
              selectList.push({
                supplierId: this.data.supplyList[findindex].id,supplierCode: this.data.supplyList[findindex].supplierCode,
                supplierCodeAndName: this.data.supplyList[findindex].supplierCodeAndName,
                supplierShortName: this.data.supplyList[findindex].supplierShortName,
                isEnableIndex: 0,
                firstChooseIndex: is_sx == -1 ? 1 : 0,
                skuList: [{
                  attachment: {},
                  state: 1,
                  isMainChart: 1,
                }, ],
              });
            }
          }
        }
      } else {
        if (selectList && selectList.length > 0) {
          supplyList.unshift({
            id: selectList[0].supplierId,
            supplierShortName: selectList[0].supplierShortName,
            supplierCodeAndName: selectList[0].supplierCodeAndName,
            supplierCode: selectList[0].supplierCode
          });
        }
        for (let index = 0; index < sel_ids.length; index++) {
          let findindex = supplyList.findIndex(
            (v) => v.id == sel_ids[index]
          );
          if (findindex != -1) {
            let findindex2 = selectList.findIndex(
              (v) => v.id == sel_ids[index]
            );
            if (findindex2 == -1) {
              let is_sx = selectList.findIndex(
                (v) => v.firstChooseIndex == 1
              );
              selectList = [{
                supplierId: this.data.supplyList[findindex].id,
                supplierCode: this.data.supplyList[findindex].supplierCode,
                supplierCodeAndName: this.data.supplyList[findindex].supplierCodeAndName,
                supplierShortName: this.data.supplyList[findindex].supplierShortName,
                isEnableIndex: 0,
                firstChooseIndex: is_sx == -1 ? 1 : 0,
                skuList: [{
                  attachment: {},
                  state: 1,
                  isMainChart: 1,
                }, ],
              }]
            }
          }
        }
      }
      this.setData({
        selectList: selectList,
        supplyList: supplyList
      });
      this.resetSupplyList();
      this.handleSelect()
    },
    //关闭选择供应商窗口
    closeSupplyEvent() {
      this.setData({
        show: false,
      });
      this.triggerEvent('close');
    },
    // 下拉加载数据
    nextPage(e) {
      console.log(e);
      if (e.detail.direction !== "bottom") {
        return
      }
      if (!(this.data.totalCount <= this.data.supplyList.length)) {
        const current = this.data.list_params.current + 1
        this.setData({
          'list_params.current': current,
        });
        this.getSupplyList()
      }
    },
    handleSelect() {
      const selectList = this.data.selectList
      if (this.data.multiple) {
        this.triggerEvent('handleSelect', selectList);
      } else {
        this.triggerEvent('handleSelect', selectList && selectList.length > 0 ? selectList[0] : {});
      }
    }
  }
})